import request from '@/utils/request'
import qs from 'qs'

export function userInfo(data) {
  return request({
    url: '/userInfo',
    method: 'post',
    data: qs.stringify(data)
  })
}

export function roleIdsByLoginStr(data) {
  return request({
    url: '/role_user/roleIdsByLoginStr',
    method: 'post',
    data: qs.stringify(data)
  })
}

export function login(data) {
  return request({
    url: '/login',
    method: 'post',
    data: qs.stringify(data)
  })
}

export function getMenu(data) {
  return request({
    url: '/getMenu',
    method: 'post',
    data: qs.stringify(data)
  })
}

export function sendSms(data) {
  return request({
    url: '/sendSms',
    method: 'post',
    data: qs.stringify(data)
  })
}

export function getInfo(token) {
  return request({
    url: '/info',
    method: 'get',
    params: { token }
  })
}

export function logout(data) {
  return request({
    url: '/logout',
    method: 'get',
    data: qs.stringify(data)
  })
}

// 二三级菜单对应
export function webMenu(data) {
  let menu = JSON.parse(window.localStorage.getItem('menu'))
  if(!menu) {
    this.$store.dispatch('user/logout')
    this.$router.push(`/login`)
    return
  }
  let menuData = menu.menu23
  let path = window.localStorage.getItem('path')
  let menuObj = {}
  menuData.forEach(item=>{
    if(item.children.length!=0){
      menuObj[item.url] = []
      item.children.forEach(children=>{
        menuObj[item.url].push(children.url)
      })
    }
  })
  if(path){
    return menuObj[path]
  }else{
    return []
  }
}
